package service_k;

import mapper_k.HomeUserMapper;
import mapper_k.MainUserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo_k.HomeUser;
import pojo_k.MainUser;
import util_k.SqlSessionFactoryUtils;

public class MainUserService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    //    mainuser登录
    public MainUser mainUserSelect(String mainuserName, String mainuserPassword) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //获取Mapper
        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        //调用方法
        MainUser mainUser = mainUserMapper.select(mainuserName, mainuserPassword);

        //释放资源
        sqlSession.close();

        // 返回查到的值
        return mainUser;

    }

    //根据用户名查找
    public MainUser selectByName(String mainuserName) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //获取Mapper
        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        MainUser mainUser = mainUserMapper.selectByName(mainuserName);

        sqlSession.close();

        return mainUser;
    }


    //    mainuser注册
    public void mainUserAdd(MainUser mainUser) {
        SqlSession sqlSession = factory.openSession();

        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        mainUserMapper.add(mainUser);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }

    //    mainuser删除某个家庭用户
    public void mainUserDelete(String homeuserName) {
        SqlSession sqlSession = factory.openSession();

        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        mainUserMapper.deleteByName(homeuserName);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //mainuser修改用户名和密码
    public void modify(String mainuserNameBefore, String mainuserNameAfter, String mainuserPasswordAfter) {
        SqlSession sqlSession = factory.openSession();

        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        mainUserMapper.modify(mainuserNameBefore, mainuserNameAfter, mainuserPasswordAfter);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //mainuser修改家庭余额总数
    public void modifyTotal(float total) {
        SqlSession sqlSession = factory.openSession();

        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        mainUserMapper.modifyTotal(total);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }

//    查找密码
    public String selectPassword(String mainuserName){
        SqlSession sqlSession = factory.openSession();

        MainUserMapper mainUserMapper = sqlSession.getMapper(MainUserMapper.class);

        String mainuserPassword = mainUserMapper.selectPassword(mainuserName);

        sqlSession.close();

        return  mainuserPassword;
    }


}
